package tmi.li.txclck;

import android.content.Context;
import android.content.SharedPreferences;


public class TxPref {

	public static String PREFS_NAME_TXCLCK = "TxClckPref";
	public static String PREFS_NAME_TXWTHR = "TxWthrPref";
	public static String PREFS_NAME_TXWTHRSMALL = "TxWthrPrefSmall";
	private String PREFS_NAME;
	
	public TxPref(String _PREFS_NAME) {
		PREFS_NAME = _PREFS_NAME;
	}
	
	
	//im saving ids in a long string seperated by a semikolon
	//if no ids were saved yet just save the id
	//if ids were saved append the new id to the string
	public void saveId(Context context, int appWidgetIds) {
		String ids = getIds(context);
		if(ids == null) {
			ids = String.valueOf(appWidgetIds);
		}else {
			ids = ids +";"+ String.valueOf(appWidgetIds);
		}
		saveIds(context, ids);
	}
	
	//saving the actual string with the ids seperated by a semikolon
	public void saveIds(Context context, String appWidgetIds){
		SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
		prefs.putString(PREFS_NAME, appWidgetIds);
		prefs.commit();
	}
	
	//saving the Preferences for each Widget, identified with PREFS_NAME+appwWidgetId since we need a unique 
	//for each widget
	public void savePrefs(Context  context, int appWidgetId, String preferences) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
		prefs.putString(PREFS_NAME+appWidgetId, preferences);
		prefs.commit();
	}

	public void saveAppName(Context context, int appWidgetId, String appname)  {
		SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
		prefs.putString(PREFS_NAME+"AppName"+appWidgetId, appname);
		prefs.commit();
	}
	
	//returns null or the ids ( all widgets active ) 
	public String getIds(Context context) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
        String prefix = prefs.getString(PREFS_NAME, null);
        if(prefix != null) {
        	return prefix;
        }else {
        	return null;
        }
	}
	
	public String getAppName(Context context, int id) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
        String prefix = prefs.getString(PREFS_NAME+"AppName"+id, null);    
        if(prefix != null) {
        	return prefix;     	
        }else {
        	return null;
        }
	}
	
	// returns preferences for one widget, or null if none found (shouldnt happen though)
	public String getPrefs(Context context, int appWidgetId) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
        String prefix = prefs.getString(PREFS_NAME+appWidgetId, null);    
        if(prefix != null) {
        	return prefix;     	
        }else {
        	return null;
        }
	}
	
	
	//function called when a Widget is deleted from the homescreen
	public void deletePreAndId(Context context, int id){
		deleteId(context, id);
		deletePrefs(context, id);
		deleteAppName(context, id);
	}
	
	private void deleteAppName(Context context, int id) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
		prefs.remove(PREFS_NAME+"AppName"+id);
		prefs.commit();
	}
	
	private void deletePrefs(Context context, int id) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
		prefs.remove(PREFS_NAME+id);
		prefs.commit();
	}
	
	private void deleteId(Context context, int id) {
		String ids_string = getIds(context);
		SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
		if(ids_string.contains(";")) {
			String[] ids = ids_string.split(";");
			String newids = ""; 
			for(int i = 0; i < ids.length; i++) {
				if (Integer.valueOf(ids[i]) != id) {
					if(newids.length() < 1) {
						newids = ids[i];
					}else {
						newids = newids + ";" + ids[i];
					}
				}	
			}
			saveIds(context, newids);
		}else {
			prefs.putString(PREFS_NAME, null);
			prefs.commit();
		}
		
	}
	
	
	
	
}
